<template>
	<el-dialog
		:title="typeMap[type]"
		v-model="setFormVisible"
		:append-to-body="true"
		:close-on-click-modal="false"
		width="950px"
	>
		<el-table
			v-loading="setLoading"
			:data="setForm"
			v-if="type === 'price'"
			style="margin-top: -25px"
		>
			<el-table-column label="规格" prop="title" />
			<el-table-column label="市场价 分" prop="market_price" align="right" width="120">
				<template #default="scope">
					{{ getMoneyByMinute(scope.row.market_price) }}
				</template>
			</el-table-column>
			<el-table-column label="新市场价 分" align="left" prop="market_price" width="150">
				<template #default="scope">
					<el-input-number
						v-model="scope.row.new_market_price"
						controls-position="right"
						size="small"
						:min="1"
					/>
				</template>
			</el-table-column>
			<el-table-column label="销售价 分" prop="sell_price" align="right" width="120">
				<template #default="scope">
					{{ getMoneyByMinute(scope.row.sell_price) }}
				</template>
			</el-table-column>
			<el-table-column label="新销售价 分" align="left" prop="sell_price" width="150">
				<template #default="scope">
					<el-input-number
						v-model="scope.row.new_sell_price"
						controls-position="right"
						size="small"
						:min="0"
					/>
				</template>
			</el-table-column>
			<el-table-column label="成本价 分" prop="cost_price" align="right" width="120">
				<template #default="scope">
					{{ getMoneyByMinute(scope.row.cost_price) }}
				</template>
			</el-table-column>
			<el-table-column label="新成本价 分" align="left" prop="cost_price" width="150">
				<template #default="scope">
					<el-input-number
						v-model="scope.row.new_cost_price"
						controls-position="right"
						size="small"
						:min="0"
					/>
				</template>
			</el-table-column>
		</el-table>
		<el-table
			v-loading="setLoading"
			:data="setForm"
			v-if="type === 'stock'"
			style="margin-top: -25px"
		>
			<el-table-column label="规格" prop="title" />
			<el-table-column label="当前库存" prop="stock" width="120">
				<template #default="scope">
					{{ scope.row.stock }}
				</template>
			</el-table-column>

			<el-table-column label="新 库存" prop="new_stock" width="150">
				<template #default="scope">
					<el-input-number
						v-model="scope.row.new_stock"
						controls-position="right"
						size="small"
						:min="0"
						:max="99999"
					/>
				</template>
			</el-table-column>
		</el-table>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="hideForm">取消</el-button>
				<el-button
					type="primary"
					:loading="setFormLoading"
					@click="handleGoodsSet"
					:disabled="submitDisabled"
				>修改</el-button
				>
			</div>
		</template>
	</el-dialog>
</template>

<script>
export default {
	name: "pop-set-form",
	props: {},
	emits: ["success"],
	data() {
		return {
			type: "",
			setForm: [],
			setLoading: false,
			setFormVisible: false,
			setFormLoading: false,
			typeMap: {
				price: "修改价格",
				stock: "修改库存"
			},
			goodsId: 0,
			goodsRow: {}
		};
	},
	computed: {
		submitDisabled() {
			let items = [];
			if (this.type === "price") {
				items = this.setForm.filter((item) => {
					if (
						item.new_market_price !== item.market_price ||
						item.new_sell_price !== item.sell_price ||
						item.new_cost_price !== item.cost_price
					) {
						return item;
					}
				});
			}
			if (this.type === "stock") {
				items = this.setForm.filter((item) => {
					if (item.new_stock !== item.stock) {
						return item;
					}
				});
			}

			return items.length < 1;
		}
	},
	methods: {
		async showForm(_goodsId, _type) {
			this.goodsId = _goodsId;
			const methodName = "get_form_" + _type.toLowerCase();
			console.log(methodName);
			let res = await this.$API.shop.goods[methodName]({ id: this.goodsId });
			if (res.code === 0) {
				this.setForm = res.data.map((item) => {
					item.new_market_price = parseInt(item.market_price);
					item.new_sell_price = parseInt(item.sell_price);
					item.new_cost_price = parseInt(item.cost_price);
					item.new_stock = isNaN(parseInt(item.stock)) ? 0 : parseInt(item.stock);
					return item;
				});
				this.type = _type;
				this.setFormVisible = true;
			}
			/*this.service.shop.goods[methodName]({ id: this.goodsId }).then((res) => {
				this.setForm = res.map((item) => {
					item.new_market_price = parseInt(item.market_price);
					item.new_sell_price = parseInt(item.sell_price);
					item.new_cost_price = parseInt(item.cost_price);
					item.new_stock = isNaN(parseInt(item.stock)) ? 0 : parseInt(item.stock);
					return item;
				});
				this.type = _type;
				this.setFormVisible = true;
			});*/
		},
		hideForm() {
			this.setFormVisible = false;
		},
		handleGoodsSet() {
			const methodName = "set_form_" + this.type.toLowerCase();
			let postData = {
				goods_id: this.goodsId,
				option_data: this.setForm.filter((item) => {
					// 过滤数据，只返回修改后的项
					if (item.new_market_price) {
						return item;
					}
					if (item.stock != item.new_stock) {
						return item;
					}
				})
			};
			this.$API.shop.goods[methodName](postData).then((res) => {
				if (res.code === 0) {
					this.$message.success(this.typeMap[this.type] + "成功");
					this.hideForm();
					this.$emit("success", this.getSuccessData());
				}
				else {
					this.$message.error('handleGoodsSet 失败,' + res.message);
				}
			});
		},
		getMoneyByMinute(_val) {
			// return utils.getMoneyByMinute(_val)
			return _val;
		},
		getSuccessData() {
			let stock = 0;
			let minPrice = 0;
			let maxPrice = 0;
			this.setForm.map((item) => {
				if (item.new_stock) {
					stock += item.new_stock;
				}
				if (minPrice === 0) {
					minPrice = item.new_sell_price;
				}
				if (maxPrice === 0) {
					maxPrice = item.new_sell_price;
				}
				if (item.new_sell_price) {
					if (item.new_sell_price < minPrice) {
						minPrice = item.new_sell_price;
					}
					if (item.new_sell_price > maxPrice) {
						maxPrice = item.new_sell_price;
					}
				}
			});

			return {
				stock: stock,
				min_price: minPrice,
				max_price: maxPrice
			};
		}
	}
};
</script>

<style scoped></style>
